package com.iot08.api.queue;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author cwj
 * @description
 * @date 2025/6/4 11:08
 **/
@Slf4j
public class LimitedSingleQueue {

    private static final int MAX_QUEUE_SIZE = 100000;
    private final BlockingQueue<JSONObject> queue = new LinkedBlockingQueue<>();

    // 单例实例
    private static final LimitedSingleQueue limitedSingleQueue = new LimitedSingleQueue();

    private LimitedSingleQueue(){}

    public static LimitedSingleQueue getInstance() {
        return limitedSingleQueue;
    }

    /**
     * @description 提交任务，将任务数据加入队列中
     * @author cwj
     * @param [task]
     * @return boolean
     * @date @time 2025/6/4 11:16
     **/
    public boolean submit(JSONObject task) {
        if (queue.size() >= MAX_QUEUE_SIZE) {
            log.info("队列已满，请自行执行或直接丢弃任务");
            return false;
        }
        return queue.offer(task);
    }
    
    /**
     * @description 取队列数据
     * @author cwj
     * @param []
     * @return com.alibaba.fastjson.JSONObject
     * @date @time 2025/6/4 11:24
     **/
    public JSONObject poll(){
        return queue.poll();
    }

    /**
     * @description 获取队列数据大小
     * @author cwj
     * @param []
     * @return int
     * @date @time 2025/6/4 11:24
     **/
    public int size(){
        return queue.size();
    }
}
